#include "pausestate.h"
#include "commonFunc.h"
#include <iostream>

pausestate::pausestate(ptrContainer* container, statewrapper* wrapper) :
    basestate(container, wrapper)
{
    m_curitem = 0;
    m_itemcount = 2;
}

pausestate::~pausestate(){
}

void pausestate::draw(int &drawtime){
    glClear(GL_COLOR_BUFFER_BIT);

    drawBackground(*g_container->bg);
    g_container->g->drawdownbar();

    g_container->g->drawstr("Paused", 50, 50, 16, 32, 1.0, 0.0, 0.0, 1.0);
    g_container->g->drawstr("Resume", 50, 720, 16, 32, 1.0, 1.0 * !(0 ^ m_curitem), 1.0 * !(0 ^ m_curitem), 1.0);
    g_container->g->drawstr("Quit", 155, 720, 16, 32, 1.0, 1.0 * !(1 ^ m_curitem), 1.0 * !(1 ^ m_curitem), 1.0);

    SDL_GL_SwapBuffers();
    drawtime = SDL_GetTicks();
}

bool pausestate::keyDown(SDL_Event &event){
    switch(event.key.keysym.sym){
      case SDLK_DOWN:
      case SDLK_RIGHT:
        m_curitem++;
        if(m_curitem >= m_itemcount){
            m_curitem = 0;
        }     
        break;
      case SDLK_UP:
      case SDLK_LEFT:
        m_curitem--;
        if(m_curitem < 0){
            m_curitem = m_itemcount - 1;
        }
        break;
      case SDLK_RETURN:
        if(m_curitem == 1){
            return false;
        }
        if(m_curitem == 0){
            g_statewrapper->changestate(1);
        }
      case SDLK_ESCAPE:
        break;
      default:
        break;
    }
    return true;
}

bool pausestate::update(int &endtime, int &ticktime){
    bool cont = true;
    SDL_Event event;
    while(SDL_PollEvent(&event) && cont){
        if(event.type == SDL_KEYDOWN){
            cont = keyDown(event);
        }
    }
    endtime = SDL_GetTicks();
    if(endtime > ticktime+TICK_TIME){
        ticktime += TICK_TIME;
    }
    return cont;
}

void pausestate::enterstate(){
}
